package com.lmm.lcbusiness.lcbusiness.utils;

import com.lmm.dresswisdom.lmmframe.util.LogUtils;

import io.realm.DynamicRealm;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmMigration;
import io.realm.RealmSchema;

/**
 * Created by xmgong on 2017/8/31.
 */

public class RealmUtils {
    public static Realm getRealm() {
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name("lcbusiness.realm")
                .schemaVersion(1)
//                .migration(new MyMigration())
                .build();
        return Realm.getInstance(config);
    }

    /**
     * 数据迁移操作
     */
    static class MyMigration implements RealmMigration {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            RealmSchema schema = realm.getSchema();
            LogUtils.write("migration" + "Version:" + oldVersion);
//            if (oldVersion == 1) {
//                schema.create("CategoryAttr_Relation_Realm")
//                        .addField("id", Integer.class)
//                        .addField("catId", Integer.class)
//                        .addField("attrId", Integer.class)
//                        .addField("sortNo", Integer.class);
//
//
//                schema.create("AttrRealm")
//                        .addField("attrId", Integer.class)
//                        .addField("attrName", String.class)
//                        .addField("sort", Integer.class)
//                        .addField("isMain", Integer.class)
//                        .addField("isMust", Integer.class)
//                        .addField("isDefault", Integer.class)
//                        .addField("sortNo", Integer.class)
//                        .addField("isNormal", Integer.class)
//                        .addField("inputType", String.class)
//                        .addField("dicId", Integer.class)
//                        .addField("valueType", Integer.class)
//                        .addField("categoryId", Integer.class);
//                oldVersion++;
//            }
//
//            if (oldVersion == 2) {
//                schema.get("AttrRealm")
//                        .addField("isMust", Integer.class);
//                oldVersion++;
//            }
//
//
//            if (oldVersion == 3) {
//                schema.get("AttrRealm")
//                        .addField("isMust", Integer.class);
//                oldVersion++;
//            }
        }
    }
}
